package com.lb.shopguide.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lb.shopguide.R;


/**
 * Created by kang on 2017/4/24.
 */

public class LbGoodsNumSelector extends LinearLayout {

    private ImageButton ibIncrease;
    private ImageButton ibDecrease;
    private TextView tvNumber;
    private int initNumber = 1;

    public LbGoodsNumSelector(Context context) {
        super(context);
    }

    public LbGoodsNumSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.layout_goodsnum_selector, this);
        ibIncrease = findViewById(R.id.ib_increase);
        ibDecrease = findViewById(R.id.ib_decrease);
        tvNumber = findViewById(R.id.tv_number);
        tvNumber.setText("" + initNumber);
        ibIncrease.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                initNumber = ++initNumber;
                tvNumber.setText("" + initNumber);
                if (mOnNumberChangeListener != null) {
                    mOnNumberChangeListener.onNumberChange(initNumber);
                }
            }
        });
        ibDecrease.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (initNumber > 1) {
                    initNumber = --initNumber;
                    tvNumber.setText("" + initNumber);
                    if (mOnNumberChangeListener != null) {
                        mOnNumberChangeListener.onNumberChange(initNumber);
                    }
                }
            }
        });


    }

    public int getNumber() {
        return initNumber;
    }

    public void setInitNumber(String number) {
        initNumber = Integer.parseInt(number);
        tvNumber.setText(number);
    }

    // 0-600的动画
//    public void startCountAnimation() {
//        ValueAnimator animator = ValueAnimator.ofInt(0, 600);
//        animator.setDuration(5000);
//        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            public void onAnimationUpdate(ValueAnimator animation) {
//                tvNumber.setText(animation.getAnimatedValue().toString());
//            }
//        });
//        animator.start();
//    }

    public interface OnNumberChangeListener {
        void onNumberChange(int number);
    }

    private OnNumberChangeListener mOnNumberChangeListener;

    public void setOnNumberChangeListener(OnNumberChangeListener listener) {
        mOnNumberChangeListener = listener;
    }
}
